{
 "cells": [
  {
   "cell_type": "raw",
   "id": "2821aab6",
   "metadata": {},
   "source": [
    "特征缩放:属性之间的取值范围相差较大，导致建模时模型只依赖于其中的某一个属性，而忽略了其他属性。因此需要特征选择来平衡属性之间的权重的。\n",
    "    标准化：不改变数据的分布状态。数据符合高斯分布\n",
    "    最小值最大值归一化：会改变数据的分布状态。不需要数据符合任何分布状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b304ac8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c321bc2",
   "metadata": {},
   "source": [
    "1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "276c05e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>school</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>address</th>\n",
       "      <th>famsize</th>\n",
       "      <th>Pstatus</th>\n",
       "      <th>Medu</th>\n",
       "      <th>Fedu</th>\n",
       "      <th>Mjob</th>\n",
       "      <th>...</th>\n",
       "      <th>famrel</th>\n",
       "      <th>freetime</th>\n",
       "      <th>goout</th>\n",
       "      <th>Dalc</th>\n",
       "      <th>Walc</th>\n",
       "      <th>health</th>\n",
       "      <th>absences</th>\n",
       "      <th>G1</th>\n",
       "      <th>G2</th>\n",
       "      <th>G3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>18</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "      <td>13</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>19</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>15</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>21</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>services</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>17</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>other</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>16</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>health</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>13</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID school    sex  age address famsize Pstatus  Medu  Fedu      Mjob  ...  \\\n",
       "0   0     GP    Man   18       U     GT3       A     1     4   at_home  ...   \n",
       "1   1     GP  WoMan   19       U     GT3       T     3     3   at_home  ...   \n",
       "2   2     GP    Man   21       U     GT3       A     4     1  services  ...   \n",
       "3   3     GP  WoMan   17       U     GT3       T     2     2     other  ...   \n",
       "4   4     GP    Man   16       U     GT3       A     1     4    health  ...   \n",
       "\n",
       "  famrel freetime goout Dalc  Walc  health  absences  G1  G2  G3  \n",
       "0      2        4     1    6     2       2        10   5  13  10  \n",
       "1      1        5     3    6     1       1         6   5  15  13  \n",
       "2      3        6     3    3     3       3         5   7  15  15  \n",
       "3      3        6     4    2     3       3         4  10  12  15  \n",
       "4      4        6     5    1     4       4         1   9  13  12  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('./4student-info-数据/student-info.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cde0f2b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID          0\n",
       "school      0\n",
       "sex         0\n",
       "age         0\n",
       "address     0\n",
       "famsize     0\n",
       "Pstatus     0\n",
       "Medu        0\n",
       "Fedu        0\n",
       "Mjob        0\n",
       "Fjob        0\n",
       "higher      0\n",
       "internet    0\n",
       "romantic    0\n",
       "famrel      0\n",
       "freetime    0\n",
       "goout       0\n",
       "Dalc        0\n",
       "Walc        0\n",
       "health      0\n",
       "absences    0\n",
       "G1          0\n",
       "G2          0\n",
       "G3          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db99339e",
   "metadata": {},
   "source": [
    "2.特征缩放-标准化"
   ]
  },
  {
   "cell_type": "raw",
   "id": "5aac0bae",
   "metadata": {},
   "source": [
    "注意点：\n",
    "     1.特征缩放只能对数值类型的数据进行特征缩放;\n",
    "     2.特征缩放时要求数据没有缺失值,在进行特征缩放之前一定要确定数据没有缺失值.\n",
    "     3.分类、回归问题是不对目标列数据进行标准化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c631065",
   "metadata": {},
   "outputs": [],
   "source": [
    "from  sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 提取出数值类型的列的列名\n",
    "# col = [col for col in df.columns if df[col].dtype!='object' and col!='G3'] #假设目标列为G3，如果题目中说明有目标列，或者是分类和回归问题，那么目标列一定不能做标准化，不然影响后面建模判断\n",
    "col = [col for col in df.columns if df[col].dtype!='object']\n",
    "\n",
    "# 定义规则\n",
    "model_std = StandardScaler()\n",
    "# 将规则应用到数据上\n",
    "df_std = model_std.fit_transform(df[col])\n",
    "# 转型(可选)。转型的主要目的是为方便操作，如果后续不涉及到结构化数据的按行按列操作，就可以不对数据转型\n",
    "df_std = pd.DataFrame(data=df_std,columns=col)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b6a06fe8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>school</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>address</th>\n",
       "      <th>famsize</th>\n",
       "      <th>Pstatus</th>\n",
       "      <th>Medu</th>\n",
       "      <th>Fedu</th>\n",
       "      <th>Mjob</th>\n",
       "      <th>...</th>\n",
       "      <th>famrel</th>\n",
       "      <th>freetime</th>\n",
       "      <th>goout</th>\n",
       "      <th>Dalc</th>\n",
       "      <th>Walc</th>\n",
       "      <th>health</th>\n",
       "      <th>absences</th>\n",
       "      <th>G1</th>\n",
       "      <th>G2</th>\n",
       "      <th>G3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.727671</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>0.092665</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>-1.341416</td>\n",
       "      <td>1.346409</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.784716</td>\n",
       "      <td>0.334188</td>\n",
       "      <td>-1.423026</td>\n",
       "      <td>1.428905</td>\n",
       "      <td>-0.890547</td>\n",
       "      <td>-0.889031</td>\n",
       "      <td>1.544886</td>\n",
       "      <td>-1.267224</td>\n",
       "      <td>1.020834</td>\n",
       "      <td>0.175537</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.718901</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>0.664580</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>0.445631</td>\n",
       "      <td>0.451070</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.369552</td>\n",
       "      <td>0.913154</td>\n",
       "      <td>-0.262871</td>\n",
       "      <td>1.428905</td>\n",
       "      <td>-1.471019</td>\n",
       "      <td>-1.468516</td>\n",
       "      <td>0.109051</td>\n",
       "      <td>-1.267224</td>\n",
       "      <td>1.593196</td>\n",
       "      <td>1.031552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.710132</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>1.808410</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>1.339154</td>\n",
       "      <td>-1.339609</td>\n",
       "      <td>services</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.199880</td>\n",
       "      <td>1.492120</td>\n",
       "      <td>-0.262871</td>\n",
       "      <td>-0.309547</td>\n",
       "      <td>-0.310075</td>\n",
       "      <td>-0.309547</td>\n",
       "      <td>-0.249908</td>\n",
       "      <td>-0.683488</td>\n",
       "      <td>1.593196</td>\n",
       "      <td>1.602229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.701362</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>-0.479250</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>-0.447893</td>\n",
       "      <td>-0.444270</td>\n",
       "      <td>other</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.199880</td>\n",
       "      <td>1.492120</td>\n",
       "      <td>0.317207</td>\n",
       "      <td>-0.889031</td>\n",
       "      <td>-0.310075</td>\n",
       "      <td>-0.309547</td>\n",
       "      <td>-0.608867</td>\n",
       "      <td>0.192116</td>\n",
       "      <td>0.734653</td>\n",
       "      <td>1.602229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.692592</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>-1.051165</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>-1.341416</td>\n",
       "      <td>1.346409</td>\n",
       "      <td>health</td>\n",
       "      <td>...</td>\n",
       "      <td>0.384955</td>\n",
       "      <td>1.492120</td>\n",
       "      <td>0.897284</td>\n",
       "      <td>-1.468516</td>\n",
       "      <td>0.270397</td>\n",
       "      <td>0.269937</td>\n",
       "      <td>-1.685744</td>\n",
       "      <td>-0.099752</td>\n",
       "      <td>1.020834</td>\n",
       "      <td>0.746214</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         ID school    sex       age address famsize Pstatus      Medu  \\\n",
       "0 -1.727671     GP    Man  0.092665       U     GT3       A -1.341416   \n",
       "1 -1.718901     GP  WoMan  0.664580       U     GT3       T  0.445631   \n",
       "2 -1.710132     GP    Man  1.808410       U     GT3       A  1.339154   \n",
       "3 -1.701362     GP  WoMan -0.479250       U     GT3       T -0.447893   \n",
       "4 -1.692592     GP    Man -1.051165       U     GT3       A -1.341416   \n",
       "\n",
       "       Fedu      Mjob  ...    famrel  freetime     goout      Dalc      Walc  \\\n",
       "0  1.346409   at_home  ... -0.784716  0.334188 -1.423026  1.428905 -0.890547   \n",
       "1  0.451070   at_home  ... -1.369552  0.913154 -0.262871  1.428905 -1.471019   \n",
       "2 -1.339609  services  ... -0.199880  1.492120 -0.262871 -0.309547 -0.310075   \n",
       "3 -0.444270     other  ... -0.199880  1.492120  0.317207 -0.889031 -0.310075   \n",
       "4  1.346409    health  ...  0.384955  1.492120  0.897284 -1.468516  0.270397   \n",
       "\n",
       "     health  absences        G1        G2        G3  \n",
       "0 -0.889031  1.544886 -1.267224  1.020834  0.175537  \n",
       "1 -1.468516  0.109051 -1.267224  1.593196  1.031552  \n",
       "2 -0.309547 -0.249908 -0.683488  1.593196  1.602229  \n",
       "3 -0.309547 -0.608867  0.192116  0.734653  1.602229  \n",
       "4  0.269937 -1.685744 -0.099752  1.020834  0.746214  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 合并数据\n",
    "df[col] = df_std[col]\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8ea38d51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID          0\n",
       "school      0\n",
       "sex         0\n",
       "age         0\n",
       "address     0\n",
       "famsize     0\n",
       "Pstatus     0\n",
       "Medu        0\n",
       "Fedu        0\n",
       "Mjob        0\n",
       "Fjob        0\n",
       "higher      0\n",
       "internet    0\n",
       "romantic    0\n",
       "famrel      0\n",
       "freetime    0\n",
       "goout       0\n",
       "Dalc        0\n",
       "Walc        0\n",
       "health      0\n",
       "absences    0\n",
       "G1          0\n",
       "G2          0\n",
       "G3          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be08c323",
   "metadata": {},
   "source": [
    "3.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2f1cec31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>school</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>address</th>\n",
       "      <th>famsize</th>\n",
       "      <th>Pstatus</th>\n",
       "      <th>Medu</th>\n",
       "      <th>Fedu</th>\n",
       "      <th>Mjob</th>\n",
       "      <th>...</th>\n",
       "      <th>famrel</th>\n",
       "      <th>freetime</th>\n",
       "      <th>goout</th>\n",
       "      <th>Dalc</th>\n",
       "      <th>Walc</th>\n",
       "      <th>health</th>\n",
       "      <th>absences</th>\n",
       "      <th>G1</th>\n",
       "      <th>G2</th>\n",
       "      <th>G3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>18</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "      <td>13</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>19</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>15</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>21</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>services</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>17</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>other</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>16</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>health</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>13</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID school    sex  age address famsize Pstatus  Medu  Fedu      Mjob  ...  \\\n",
       "0   0     GP    Man   18       U     GT3       A     1     4   at_home  ...   \n",
       "1   1     GP  WoMan   19       U     GT3       T     3     3   at_home  ...   \n",
       "2   2     GP    Man   21       U     GT3       A     4     1  services  ...   \n",
       "3   3     GP  WoMan   17       U     GT3       T     2     2     other  ...   \n",
       "4   4     GP    Man   16       U     GT3       A     1     4    health  ...   \n",
       "\n",
       "  famrel freetime goout Dalc  Walc  health  absences  G1  G2  G3  \n",
       "0      2        4     1    6     2       2        10   5  13  10  \n",
       "1      1        5     3    6     1       1         6   5  15  13  \n",
       "2      3        6     3    3     3       3         5   7  15  15  \n",
       "3      3        6     4    2     3       3         4  10  12  15  \n",
       "4      4        6     5    1     4       4         1   9  13  12  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('./4student-info-数据/student-info.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10201c70",
   "metadata": {},
   "source": [
    "3.特征缩放-最小值最大值归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2907d980",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "# 提取出数值类型的列的列名\n",
    "# col = [col for col in df.columns if df[col].dtype!='object' and col!='G3'] #假设目标列为G3，如果题目中说明有目标列，或者是分类和回归问题，那么目标列一定不能做标准化，不然影响后面建模判断\n",
    "col = [col for col in df.columns if df[col].dtype!='object'] \n",
    "\n",
    "model_mm = MinMaxScaler()\n",
    "df_mm = model_mm.fit_transform(data[col])\n",
    "df_mm = pd.DataFrame(data=df_mm,columns=col)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "61e774b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>school</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>address</th>\n",
       "      <th>famsize</th>\n",
       "      <th>Pstatus</th>\n",
       "      <th>Medu</th>\n",
       "      <th>Fedu</th>\n",
       "      <th>Mjob</th>\n",
       "      <th>...</th>\n",
       "      <th>famrel</th>\n",
       "      <th>freetime</th>\n",
       "      <th>goout</th>\n",
       "      <th>Dalc</th>\n",
       "      <th>Walc</th>\n",
       "      <th>health</th>\n",
       "      <th>absences</th>\n",
       "      <th>G1</th>\n",
       "      <th>G2</th>\n",
       "      <th>G3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.2</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.583333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.002538</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>at_home</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.555556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.833333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.005076</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>services</td>\n",
       "      <td>...</td>\n",
       "      <td>0.4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.444444</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.007614</td>\n",
       "      <td>GP</td>\n",
       "      <td>WoMan</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>T</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>other</td>\n",
       "      <td>...</td>\n",
       "      <td>0.4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.583333</td>\n",
       "      <td>0.750000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.010152</td>\n",
       "      <td>GP</td>\n",
       "      <td>Man</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>U</td>\n",
       "      <td>GT3</td>\n",
       "      <td>A</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>health</td>\n",
       "      <td>...</td>\n",
       "      <td>0.6</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>0.750000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         ID school    sex       age address famsize Pstatus      Medu  \\\n",
       "0  0.000000     GP    Man  0.333333       U     GT3       A  0.000000   \n",
       "1  0.002538     GP  WoMan  0.500000       U     GT3       T  0.666667   \n",
       "2  0.005076     GP    Man  0.833333       U     GT3       A  1.000000   \n",
       "3  0.007614     GP  WoMan  0.166667       U     GT3       T  0.333333   \n",
       "4  0.010152     GP    Man  0.000000       U     GT3       A  0.000000   \n",
       "\n",
       "       Fedu      Mjob  ... famrel freetime goout Dalc  Walc  health  absences  \\\n",
       "0  1.000000   at_home  ...    0.2      0.6   0.0  1.0   0.2     0.2  1.000000   \n",
       "1  0.666667   at_home  ...    0.0      0.8   0.4  1.0   0.0     0.0  0.555556   \n",
       "2  0.000000  services  ...    0.4      1.0   0.4  0.4   0.4     0.4  0.444444   \n",
       "3  0.333333     other  ...    0.4      1.0   0.6  0.2   0.4     0.4  0.333333   \n",
       "4  1.000000    health  ...    0.6      1.0   0.8  0.0   0.6     0.6  0.000000   \n",
       "\n",
       "         G1        G2        G3  \n",
       "0  0.166667  0.833333  0.583333  \n",
       "1  0.166667  1.000000  0.833333  \n",
       "2  0.333333  1.000000  1.000000  \n",
       "3  0.583333  0.750000  1.000000  \n",
       "4  0.500000  0.833333  0.750000  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[col] = df_mm[col]\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7ca1133",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66f8e4a3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e74b1938",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3a2acb2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
